package pers.vinsonws.jianghu.clientwin.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import pers.vinsonws.jianghu.clientwin.global.Info;
import pers.vinsonws.jianghu.codeCompont.MsgEveryPro;
import pers.vinsonws.jianghu.tools.MergeMessage;

/**
 * @auther vinsonws
 * @date 2018/11/4 21:26
 */
public class ChatClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Info.channel = ctx.channel();
        System.out.println("客户端建立连接");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Info.channel = null;
        ctx.close();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        MsgEveryPro.Message message = (MsgEveryPro.Message) msg;
        System.out.println("ChatClientHandler:" + message.getMessage());
        ClientMainReadProcess.readProcess(message, ctx);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.err.println("意外错误！");
        cause.printStackTrace();
    }
}
